package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        System.out.println(redisTemplate.opsForValue().get("\\xAC\\xED\\x00\\x05t\\x00\\x04name"));
        //得到用户手机号
        String phone = user.getPhone();
        //生成随机验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //验证码打印在控制台
        log.info("code={}", code);
        //验证码存储在session域中
        //request.getSession().setAttribute("code", code);
        //验证码存储在redis中
        redisTemplate.opsForValue().set("code" + phone, code, 2, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("name", "liyang");
        return R.success("输入验证码");
    }


    @PostMapping("/login")
    public R<User> sendMsg(@RequestBody Map<String, String> map, HttpServletRequest request) {
        //获得客户端传入的参数
        System.out.println("----------------------");
        System.out.println(redisTemplate.opsForValue().get("name"));
        redisTemplate.delete("name");
        String phone = map.get("phone");
        String code = map.get("code");
        //判断用户传入的验证码是否正确
//        String sessionCode = (String) request.getSession().getAttribute("code").toString();
        //将redis数据库的验证码提取出来做比对
        String sessionCode = redisTemplate.opsForValue().get("code" + phone) + "";
        if (!StringUtils.equals(code, sessionCode)) {
            return R.error("验证码错误");
        }
        //用户登录成功,判断用户是否是新用户
        //因为用户登录成功,销毁redis中的验证码
//        redisTemplate.delete("code" + phone);
        LambdaQueryWrapper<User> wrap = new LambdaQueryWrapper<>();
        wrap.eq(User::getPhone, phone);
        User user = userService.getOne(wrap);
        //查询到的用户为null,证明是新用户,添加到数据库
        User user1 = new User();
        if (user == null) {
            user1.setPhone(phone);
            userService.save(user1);
            request.getSession().setAttribute("user", user1.getId());
        }
        //将用户ID存储在session域中
        request.getSession().setAttribute("user", user.getId());
        return R.success(user1);
    }

}
